package cn.iocoder.yudao.module.erp.controller.admin.business.produce.produceprocess;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.erp.controller.admin.business.produce.produceprocess.vo.ProduceProcessPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.business.produce.produceprocess.vo.ProduceProcessRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.business.produce.produceprocess.vo.ProduceProcessSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.business.produce.produceprocess.ProduceProcessDO;
import cn.iocoder.yudao.module.erp.service.business.produce.produceprocess.ProduceProcessService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 工序")
@RestController
@RequestMapping("/erp/produce-process")
@Validated
public class ProduceProcessController {

    @Resource
    private ProduceProcessService produceProcessService;

    @PostMapping("/create")
    @Operation(summary = "创建工序")
    @PreAuthorize("@ss.hasPermission('erp:produce-process:create')")
    public CommonResult<Long> createProduceProcess(@Valid @RequestBody ProduceProcessSaveReqVO createReqVO) {
        return success(produceProcessService.createProduceProcess(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新工序")
    @PreAuthorize("@ss.hasPermission('erp:produce-process:update')")
    public CommonResult<Boolean> updateProduceProcess(@Valid @RequestBody ProduceProcessSaveReqVO updateReqVO) {
        produceProcessService.updateProduceProcess(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除工序")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:produce-process:delete')")
    public CommonResult<Boolean> deleteProduceProcess(@RequestParam("id") Long id) {
        produceProcessService.deleteProduceProcess(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得工序")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:produce-process:query')")
    public CommonResult<ProduceProcessRespVO> getProduceProcess(@RequestParam("id") Long id) {
        ProduceProcessDO produceProcess = produceProcessService.getProduceProcess(id);
        return success(BeanUtils.toBean(produceProcess, ProduceProcessRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得工序分页")
    @PreAuthorize("@ss.hasPermission('erp:produce-process:query')")
    public CommonResult<PageResult<ProduceProcessRespVO>> getProduceProcessPage(@Valid ProduceProcessPageReqVO pageReqVO) {
        PageResult<ProduceProcessDO> pageResult = produceProcessService.getProduceProcessPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProduceProcessRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出工序 Excel")
    @PreAuthorize("@ss.hasPermission('erp:produce-process:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportProduceProcessExcel(@Valid ProduceProcessPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProduceProcessDO> list = produceProcessService.getProduceProcessPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "工序.xls", "数据", ProduceProcessRespVO.class,
                        BeanUtils.toBean(list, ProduceProcessRespVO.class));
    }

}